package com.hspedu.hspliving.commodity.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hspedu.common.valid.SaveGroup;
import com.hspedu.common.valid.UpdateGroup;
import com.hspedu.common.valid.UpdateIsshowGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hspedu.hspliving.commodity.entity.BrandEntity;
import com.hspedu.hspliving.commodity.service.BrandService;
import com.hspedu.common.utils.PageUtils;
import com.hspedu.common.utils.R;



/**
 * 家居品牌
 *
 * @author hsp
 * @email hsp@gmail.com
 * @date 2025-07-17 12:32:55
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("commodity:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * 1. @Validated 标注在 这里表示启用对 BrandEntity 字段的校验
     * 2. 注意: 如果不写 @Validated 即使在 BrandEntity 写了校验注解, 也不会生效
     * 3. BindingResult result: springboot 会将校验的结果 放入到 result
     * 4. 程序员可以通过 BindingResult result 取出自己关系的错误信息
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:brand:save")
    /**
     * 老师解读
     * 1. @Validated({SaveGroup.class}) 表 示 在 执 行 savp API 接 口 时 ， 走 的 是
     SaveGroup.class 校验规则
     */
    public R save(@Validated({SaveGroup.class}) @RequestBody BrandEntity brand, BindingResult result){
//        HashMap<String, String> map = new HashMap<>();
//        if (result.hasErrors()) {
//            result.getFieldErrors().forEach(error -> map.put(error.getField(), error.getDefaultMessage()));
//            return R.error(400,"非法数据").put("data", map);
//        }
//        else  {
            brandService.save(brand);
            return R.ok();
//        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/update/isshow")
//    @RequiresPermissions("commodity:brand:update")
    public R updateIsshow(@Validated({UpdateIsshowGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
